<script setup>
import { ref, onMounted } from 'vue'
import PageFullContainer from '@/components/pageFullContainer.vue'
import {
  getEvaluateService,
  deleteEvaluateService,
  searchEvaluateService
} from '@/apis/evaluate'

// 存储用户评价记录
const EvaluateList = ref([])

// 实现分页需要的参数
const pageSize = ref(3)
const page = ref(1)
const total = ref(null)

// 获取评价记录需要的参数
const evaluateItem = ref({
  pageSize: pageSize.value,
  page: page.value
})

// 获取用户评价记录
const getEvaluate = async () => {
  isLoading.value = true
  const res = await getEvaluateService(evaluateItem.value)
  EvaluateList.value = res.data.data.rows
  total.value = res.data.data.count
  isLoading.value = false
}

onMounted(() => {
  getEvaluate()
})

// 控制loading加载的开启和关闭
const isLoading = ref(false)

// 处理页码变化
const handleCurrentChange = (newPage) => {
  evaluateItem.value.page = newPage
  getEvaluate()
}

// 处理页面显示数据条数的变化
const handleSizeChange = (newSize) => {
  evaluateItem.value.pageSize = newSize
  getEvaluate()
}

// 删除评论
const deleteEvaluate = async (id) => {
  await ElMessageBox.confirm('你确定要移除这条评论记录吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })

  // 删除评论
  await deleteEvaluateService(id)
  ElMessage.success('删除成功')
  getEvaluate()
}

// 搜索框内输入的内容
const SearchInput = ref(null)

// 搜索功能
const handleSearch = async () => {
  const res = await searchEvaluateService(SearchInput.value)
  EvaluateList.value = res.data.data
}

// 重置功能
const handleReset = () => {
  SearchInput.value = null
  getEvaluate()
}
</script>

<template>
  <PageFullContainer title="评价记录">
    <template #extra>
      <div class="extra">
        <div class="search">
          <el-input
            v-model="SearchInput"
            style="width: 240px"
            placeholder="请输入搜索内容"
            @keyup.enter="handleSearch"
            clearable
          />
          <el-button type="primary" @click="handleSearch">
            <template #default>搜索</template>
            <template #icon>
              <el-icon><i-ep-Search></i-ep-Search></el-icon>
            </template>
          </el-button>
          <el-button type="info" @click="handleReset">
            <template #default>重置</template>
            <template #icon>
              <el-icon><i-ep-Refresh></i-ep-Refresh></el-icon>
            </template>
          </el-button>
        </div>
      </div>
    </template>

    <!-- 评价记录 -->
    <div class="content" v-if="EvaluateList.length > 0">
      <el-card
        style="max-width: 480px"
        v-for="item in EvaluateList"
        :key="item.id"
        shadow="hover"
        class="card"
      >
        <template #header>
          <div class="card-header">
            <div class="image">
              <img :src="item.property.image_url.interior_img[0].url" alt="" />
            </div>
            <span>{{ item.property.title }}</span>
          </div>
        </template>
        <div class="content">
          <el-rate v-model="item.rating" size="large" disabled />
          <p>{{ item.comment }}</p>
        </div>
        <template #footer>
          <div class="card-footer">
            {{ item.user.username }}
            <el-button type="danger" circle @click="deleteEvaluate(item.id)">
              <template #icon>
                <i-ep-Delete></i-ep-Delete>
              </template>
            </el-button>
          </div>
        </template>
      </el-card>
    </div>

    <el-empty v-else :image-size="200" />

    <template #footer>
      <!-- 分页区域 -->
      <el-pagination
        background
        layout="jumper,total,sizes,prev,pager,next"
        :page-sizes="[2, 3]"
        v-model:page-size="pageSize"
        v-model:current-page="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </template>
  </PageFullContainer>
</template>

<style lang="scss" scoped>
.extra {
  width: 1250px;
  display: flex;
  justify-content: space-between;

  .search {
    button {
      margin: 0 10px;
    }
  }
}

.content {
  display: flex;
  justify-content: space-around;

  .card {
    width: 400px;
    height: 600px;
    margin: 10px;

    .card-header {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text-align: center;
      width: 350px;
      height: 180px;

      .image {
        width: 200px;
        height: 120px;
        margin-bottom: 10px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      span {
        font-size: 16px;
        margin: 5px 0;
      }
    }

    .content {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      height: 220px;

      p {
        margin-top: 10px;
      }

      ::v-deep .el-icon {
        font-size: 30px;
      }
    }

    .card-footer {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
